﻿using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Mvc;
using CozeDomain.IServices;
using D.UtilFreesqlOrm;
using CozeDto.SysDicDtos;
using Microsoft.AspNetCore.Authorization;
using CozeDomain.Entities;
using CozeDomain.Services;
using CozeDto.ProjectDtos;

namespace CozeWebApi
{
    /// <summary>
    /// 字典模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class SysDicController : BaseApiController
    {
        private readonly ISysDicService _sysDicService;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="sysDicService"></param>
        public SysDicController(ISysDicService sysDicService)
        {
            _sysDicService = sysDicService;
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepQuerySysDicTreeTable>>> Pages([FromBody] ReqQuerySysDicPage query)
        {
            return await _sysDicService.GetPagesAsync(query);
        }

        /// <summary>
        /// 获取下拉
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepSelect>>> QueryDicSelect([FromQuery] string dicTypeUniqueCode)
        {
            return await _sysDicService.QueryDicSelect(dicTypeUniqueCode);
        }

        /// <summary>
        /// 获取下拉树
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepTreeSelect>>> QueryDicTreeSelect([FromQuery] string dicTypeUniqueCode)
        {
            return await _sysDicService.QueryDicTreeSelect(dicTypeUniqueCode);
        }

        /// <summary>
        /// 获取下拉树-2
        /// </summary>
        /// <param name="dicTypeUniqueCode"></param>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepTreeList>>> QueryDicTreeSelect2([FromQuery] string dicTypeUniqueCode)
        {
            return await _sysDicService.QueryDicTreeSelect2(dicTypeUniqueCode);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddSysDic input)
        {
            input.CreateUser = Account;
            return await _sysDicService.AddSysDicAsync(input);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        [UnitOfWork]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _sysDicService.DeleteSysDicAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateSysDic reqUpdateProject)
        {
            reqUpdateProject.Id = id;
            reqUpdateProject.UpdateUser = Account;
            return await _sysDicService.UpdateSysDicAsync(reqUpdateProject);
        }

        /// <summary>
        /// 根据ID主键获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepSysDicInfo>> Get(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = LoginId;
            }
            return await _sysDicService.GetSysDicAsync(id);
        } 
    }
}